<template>
    <div class="table-all"">
        <!-- 搜索区 -->
        <div class="app-container search-box">
            <SearchForm
                    :form="searchData.form"
                    :options="searchData.options"
                    @searchUpdate="search"
                    @resetSearchData="resetSearchData"
            ></SearchForm>
        </div>
        <!-- 按钮区 -->
        <div class="app-container table-box">
            <div class="table-main">
                <!-- 表格 -->
                <v-table
                  title="便名便企信息申请数据"
                        v-if="tableMainHeight>0"
                        :table-columns="tableColumns"
                        :table-data="tableData"
                        :loading="loading"
                        :is-index="true"
                        ref="table"
                        :maxHeight="tableMainHeight">
                    <template #topButtonList>
                        <ButtonForm :buttonList="buttonList" @clickButton="clickButton"></ButtonForm>
                    </template>
                  <template #status="scope">
                    {{getDictValue(scope.row.status, statusOptions)}}
                  </template>
                    <template #operation="scope">
                        <a v-hasPermission="['publicPeopleApply:add']" class="table-btn-edit table-operation"
                           @click="handleEdit(scope.row)">{{$t('table.edit')}}</a>
                      <a v-hasPermission="['publicPeopleApply:detail']" class="table-btn-view table-operation"
                         @click="handleDetail(scope.row)">{{$t('table.view')}}</a>
                        <a v-hasPermission="['publicPeopleApply:delete']" class="table-btn-delete table-operation"
                           @click="singleDelete(scope.row)">{{$t('table.delete')}}</a>
                    </template>
                </v-table>
            </div>
            <div class="table-page">
                <!-- 页码 -->
                <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize"
                            @pagination="search"/>
            </div>
        </div>
        <!-- 详情 -->
        <detailDialog :dialog-visible="detailDialogShow"
                    @success="editSuccess"
                    @close="editClose"
                    :openType="detailOpenType"
                    :id="detailId"
                    :title="detailTitle"
        ></detailDialog>
    </div>
</template>

<script>
    import { publicPeopleApplyPage,
            publicPeopleApplyExport,
            publicPeopleApplyDelete } from '@/api/risk/publicPeopleApply'
    import SearchForm from '@/components/searchForm/index'
    import ButtonForm from '@/components/ButtonForm/index'
    import VTable from '@/components/VTable/index'
    import Pagination from '@/components/Pagination/index'
    import detailDialog from './detail.vue'
    export default {
        components:{
            VTable,Pagination,detailDialog,SearchForm,ButtonForm
        },
        data(){
            return{
                tableColumns:[
                            {
                                prop: 'pname',
                                label: '申请人',
                                minWidth: '150',
                                slotname: 'pname',
                                options: {}
                            },

                            {
                                prop: 'applyTime',
                                label: '申请时间',
                                minWidth: '150',
                                slotname: 'applyTime',
                                options: {}
                            },
                            {
                              prop: 'basicInfoName',
                              label: '类型',
                              minWidth: '150',
                              slotname: 'basicInfoName',
                              options: {}
                            },
                            {
                              prop: 'areaCode',
                              label: '行政区',
                              minWidth: '150',
                              slotname: 'areaCode',
                              options: {}
                            },
                            {
                                prop: 'uname',
                                label: '请求协助单位',
                                minWidth: '150',
                                slotname: 'uname',
                                options: {}
                            },
                            {
                                prop: 'xtime',
                                label: '协助日期',
                                minWidth: '150',
                                slotname: 'xtime',
                                options: {}
                            },
                            {
                                prop: 'name',
                                label: '事项名称',
                                minWidth: '150',
                                slotname: 'name',
                                options: {}
                            },
                            {
                              prop: 'phone',
                              label: '申请人联系方式',
                              minWidth: '150',
                              slotname: 'phone',
                              options: {}
                            },

                            {
                                prop: 'memo',
                                label: '协助说明',
                                minWidth: '150',
                                slotname: 'memo',
                                options: {}
                            },
                            {
                              prop: 'status',
                              label: '状态',
                              minWidth: '150',
                              slotname: 'status',
                              options: {}
                            },
                    {
                        prop: 'operation',
                        label: '操作',
                        width: '200',
                        align: 'center',
                        slotname: 'operation',
                        overflow: false,
                        fixed: 'right'
                    }
                ],
                tableData:[],
                selection: [],
                loading:false,
                total:1,
                pageNum:1,
                pageSize:10,
                detailOpenType:"add",
                detailDialogShow:false,
                detailId:"",
                detailTitle:"新增",
                dict:{},
                searchData:{
                  form:{
                        pname:'',
                        name:'',
                },
                   options:[
                             {
                                 title:'申请人',
                                 key: "pname",
                                 type: "text",
                             },
                             {
                                 title:'事项名称',
                                 key: "name",
                                 type: "text",
                             },
                   ]
                },
                buttonList:[
                    {name:'新增',methods:'handleAdd',type:'primary',haspremission:['publicPeopleApply:add']},

                    //{name:'导入',methods:'handleImport'},
                    //{name:'导出',methods:'handleExport'}
                ],
                tableMainHeight: 0,
                // 字典
                statusOptions:[],
            }
        },
        watch: {
          'statusOptions':{
            handler(newVal, oldVal) {
              this.searchData.options.forEach(t=>{
                if(t.key=='status'){
                  t.option.data = newVal
                }
              })
            },
            deep: true,
            immediate: true
          },
        },
        methods:{
            getCheckBoxValue(value,option){
                if(!value){
                    return
                }
                let temp = []
                let data = value.split(",")
                data.forEach(t=>{
                    let item = option.find(x=> x.value == t)
                if(item != undefined){
                    temp.push(item.label)
                }
            })
                return temp.join(",")
            },
            getDictValue(value,option){
                let item = option.find(t=> t.value == value)
                if(item!=undefined){
                    return item.label
                }
                return value
            },
            editClose(){
                this.detailDialogShow = false
            },
            editSuccess(){
                this.search()
                this.detailDialogShow = false
            },
            search(){
                this.fetch({
                        ...this.searchData.form,
                })
            },
            fetch(params={}){
                params.pageSize = this.pageSize
                params.pageNum = this.pageNum
                this.loading = true
                 publicPeopleApplyPage(params).then(async res=>{
                    if(res.code==200){
                        this.total = res.data.total
                        let rows = res.data.rows
                        for(let item of rows){
                          item.areaCode = await this.$commonJs.areaCodeToName(item)
                        }
                        this.tableData = res.data.rows
                    }else{
                    this.$message({
                        message: this.$t('tips.systemException'),
                        type: 'error'
                     })
                   }
                }).finally(final=>{
                    this.loading = false
               })
            },
            // 重置搜索条件
            resetSearchData(){
                this.searchData.form = {
                        pname:'',
                        name:'',
                }
            },

            // 触发按钮区域按钮方法
            clickButton(methods){
                this[methods]()
            },
            handleAdd(){
                this.detailOpenType = "add"
                this.detailTitle = "新增"
                this.detailDialogShow = true
            },
            handleEdit(row){
                this.detailId = row.id
                this.detailOpenType = "edit"
                this.detailTitle="编辑"
                this.detailDialogShow = true
            },
            handleDetail(row){
              this.detailId = row.id
              this.detailOpenType = "detail"
              this.detailTitle="详情"
              this.detailDialogShow = true
            },
            handleExport(){
                let params = {
                        ...this.searchData.form
                }
                publicPeopleApplyExport(params,`便民便企信息申请_${new Date().getTime()}.xlsx`);
            },
            handleImport(){
                console.log("等待处理")
            },
            closeDialog(){
                this.detailDialogShow = false
            },

            singleDelete(row){
                this.$confirm(this.$t('tips.confirmDelete'), this.$t('common.tips'), {
                    confirmButtonText: this.$t('common.confirm'),
                    cancelButtonText: this.$t('common.cancel'),
                    type: 'warning'
                }).then(() => {
                    this.loading = true
                    publicPeopleApplyDelete(row.id).then(res=>{
                    if(res.code==200){
                    this.$message({
                        message: this.$t('tips.deleteSuccess'),
                        type: 'success'
                    })
                    this.search()
                     }else{
                        this.$message({
                            message: this.$t('tips.systemException'),
                            type: 'error'
                        })
                    }
                 }).finally(final=>{
                    this.loading = false
                 })
                })
            }
        },
        activated(){
            this.fetch()
        },
        created(){
            // 获取字典值


          this.statusOptions = []
          this.$commonJs.filterDataDict("publicPeopleApplyStatus",this.statusOptions )

        },
        mounted(){
            this.tableMainHeight = document.getElementsByClassName('table-main')[0].clientHeight - 44 //减去顶部按钮列表的高度
        }
    }
</script>

<style lang="scss" scoped>
    .table-all {
        display: flex;
        flex-direction: column;
        height: 100%;

    .search-box {
        padding-bottom: 6px;
    }

    .table-box {
        padding-top: 10px;
        flex: 1;
        display: flex;
        flex-direction: column;

    .table-main {
        flex: 1;
        overflow: auto;
    }

    .table-page {
        // padding-top: 10 px;
        // padding-bottom: 10 px;
    }

    ::v-deep .pagination-container {
        text-align: right;
    }

    }
    }
</style>
